<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iterator概念</title>
</head>
<body>
    <h1>Iterator（遍历器）的概念</h1>
    <h2>为各种数据结构提供统一的访问机制</h2>

    <script>
        // 模拟Iterator的实现
        function makeIterator(array) {
            let nextIndex = 0;
            return {
                next: function() {
                    return nextIndex < array.length ?
                        {value: array[nextIndex++], done: false} :
                        {value: undefined, done: true};
                }
            };
        }

        // 使用示例
        const it = makeIterator(['a', 'b', 'c']);
        console.log(it.next()); // { value: "a", done: false }
        console.log(it.next()); // { value: "b", done: false }
        console.log(it.next()); // { value: "c", done: false }
        console.log(it.next()); // { value: undefined, done: true }

        // ES6内置的Iterator接口
        const arr = ['x', 'y', 'z'];
        const iter = arr[Symbol.iterator]();
        
        console.log(iter.next()); // { value: 'x', done: false }
        console.log(iter.next()); // { value: 'y', done: false }
        console.log(iter.next()); // { value: 'z', done: false }
        console.log(iter.next()); // { value: undefined, done: true }
    </script>
</body>
</html> 